
import argparse
import pandas as pd
import struct

def readBmp(content):
    data=[]
    row_list = content.values.tolist()

    for row in row_list:
        for cell_str in row:
            if(isinstance(cell_str,str)):
                cell_int = int(cell_str, 16)
                data.append(cell_int)
    return data

parser = argparse.ArgumentParser(description='Jenkins pipline parameters')

parser.add_argument('--line', type=int, default=8, help='lines count of each bmp')
parser.add_argument('--file', type=str, default='result.csv', help='CSV file of test result')
parser.add_argument('--uuid', type=str, default='', help='uuid list of all bmp')
parser.add_argument('--order', type=str, default='', help='uuid list of all bmp')
parser.add_argument('--bmpsize', type=int, default=360, help='file size of each bmp')
parser.add_argument('--outfile', type=str, default="bmp.bin", help='hex file of all bmp')
parser.add_argument('--eepromSize', type=int, default=3584, help='hex file of all bmp')
args = parser.parse_args()

print("bmp lines:",args.line)
print("eepromSize:",args.eepromSize)

stop = 0
content = pd.read_csv(args.file, sep=',',skiprows=0,header=None)
line_count = len(content)
print("file lines:",line_count)

bmp_count=0
all_data=[]
all_bmp_data=[]
for start in range(0,line_count,args.line):
    stop = start + args.line
    bmp_data = readBmp(content[start:stop])
    all_bmp_data += bmp_data
    bmp_count += 1

all_data += all_bmp_data
print("bmp_count:",bmp_count)

uuid_list_str=args.uuid.split(sep=",")
uuid_list=[]
for index in range(0,len(uuid_list_str)):
    uuid_list.append(int(uuid_list_str[index], 16))

print(bmp_count)

if len(uuid_list) != bmp_count:
    print("图片与uuid数目不匹配")
    exit(-1)

print("uuidlist",uuid_list)

order_list_str=args.order.split(sep=",")
order_list=[]
for index in range(0,len(order_list_str)):
    order_list.append(int(order_list_str[index], 16))

if len(order_list) != bmp_count:
    print("图片与order数目不匹配")
    exit(-1)

print(order_list)

infoStartAddr = args.eepromSize - 512
fillLength = infoStartAddr - len(all_bmp_data)
print("infoStartAddr:",infoStartAddr)
print("len(all_bmp_data):",len(all_bmp_data))
print("fillLength:",fillLength)

#fill 0xFF
bmp_fill_data=[]
for start in range(0,fillLength):
    bmp_fill_data.append(0xFF)

all_data += bmp_fill_data

#version
all_data.append(0x01)
#STORAGE_INIT_MAGIC_NUM 0xBEEF
all_data.append(0xBE)
all_data.append(0xEF)
#bmpNumber
all_data.append(bmp_count)

maxBmpNum = (args.eepromSize - 512) // args.bmpsize
print("maxBmpNum:",maxBmpNum)

#pics
picinfo = []
for index in range(0,bmp_count):
    picinfo.append(uuid_list[index]>>8)
    picinfo.append(uuid_list[index]&0xFF)
    addr = args.bmpsize*index
    picinfo.append(addr>>8)
    picinfo.append(addr&0xFF)

all_data += picinfo

#fill valid pics info
pic_fill_data = []
if(bmp_count < maxBmpNum):
    for index in range(0,maxBmpNum - bmp_count):
        #define INVALID_BMP_UUID 0xFFFF
        pic_fill_data.append(0xFF)
        pic_fill_data.append(0xFF)
        #define INVALID_BMP_ADDR 0xFFFF
        pic_fill_data.append(0xFF)
        pic_fill_data.append(0xFF)

all_data += pic_fill_data

#bmpDisplayList
bmp_display_list = []
for index in range(0,bmp_count):
    bmp_display_list.append(order_list[index]>>8)
    bmp_display_list.append(order_list[index]&0xFF)

all_data += bmp_display_list

print(bmp_display_list)

outfile = open(args.outfile,"wb")
for i in all_data:
    s = struct.pack('B', i)
    outfile.write(s)
outfile.close()
